function changeByte(limit) {
    let size = "";
    if (limit < 0.1 * 1024) {
        size = limit.toFixed(2) + " B"
    } else if (limit < 0.1 * 1024 * 1024) {
        size = (limit / 1024).toFixed(2) + " KB"
    } else if (limit < 0.1 * 1024 * 1024 * 1024) {
        size = (limit / (1024 * 1024)).toFixed(2) + " MB"
    } else {
        size = (limit / (1024 * 1024 * 1024)).toFixed(2) + " GB"
    }
    let sizeStr = size + "";
    let index = sizeStr.indexOf(".");
    let dou = sizeStr.substr(index + 1, 2)
    if (dou == "00") {
        return sizeStr.substring(0, index) + sizeStr.substr(index + 3, 2)
    }
    return size;
}

function checkLogin() {
    let _u_token = u_token
    if (!_u_token) {
        $('.jsOpenLoginLayer').trigger('click')
        return false
    }
    return true
}

function checkVip() {
    let _is_vip = is_vip
    if (_is_vip != '1') {
        $('.op-vip').trigger('click')
        return false
    }
    return true
}

function layerSuccess(msg, time) {
    if (!time) time = 1500
    layer.msg(msg, {
        icon: 1,
        shade: [0.3, '#000'], time: time,
        shadeClose: true
    })
}

function layerFailed(msg, time) {
    if (!time) time = 1500
    layer.msg(msg, {
        icon: 2,
        shade: [0.3, '#000'], time: time,
        shadeClose: true
    })
}

/**
 * 获取 blob
 * @param  {String} url 目标文件地址
 * @return {Promise}
 */
function getBlob(url) {
    return new Promise(resolve => {
        const xhr = new XMLHttpRequest();

        xhr.open('GET', url, true);
        xhr.setRequestHeader('Access-Control-Allow-Origin', '*');
        xhr.responseType = 'blob';
        xhr.onload = () => {
            if (xhr.status === 200) {
                resolve(xhr.response);
            }
        };

        xhr.send();
    });
}

/**
 * 保存
 * @param  {Blob} blob
 * @param  {String} filename 想要保存的文件名称
 */
function saveAs(blob, filename) {
    if (window.navigator.msSaveOrOpenBlob) {
        navigator.msSaveBlob(blob, filename);
    } else {
        const link = document.createElement('a');
        const body = document.querySelector('body');

        link.href = window.URL.createObjectURL(blob);
        link.download = filename;

        // fix Firefox
        link.style.display = 'none';
        body.appendChild(link);

        link.click();
        body.removeChild(link);

        window.URL.revokeObjectURL(link.href);
    }
}

/**
 * 下载
 * @param  {String} url 目标文件地址
 * @param  {String} filename 想要保存的文件名称
 * @param  {Function} callback
 */
function download(url, filename, callback) {
    getBlob(url).then(blob => {
        saveAs(blob, filename);
    });
    if (typeof callback == "function")
        callback();
}

/**
 *
 * @param s
 * @returns {boolean}
 */
function checkMobile(s) {
    return (/^1(3|4|5|6|7|8|9)\d{9}$/.test(s))
}

function loginOut() {
    layer.alert('确认退出？', {
        title: '退出',
        btn: ['退出', '取消'],
        closeBtn: 2,
        shadeClose: true,
        yes: function () {
            location.href = '/login/out'
        }
    })
}

$(function () {
    $('#go-back-top').click(function () {
        let $page = $('html,body')
        //兼容chrome和ie，获取滚动条当前距离顶部的距离
        let distance = $('html').scrollTop() + $('body').scrollTop()
        // 总时间
        let time = 200
        // 间隔时间
        let intervalTime = 10
        //单元移动的距离，每一次移动的距离
        //一共需要移动的次数=(time/intervalTime)
        //每次需要移动的距离=distance/(time/intervalTime)
        let itemDistance = distance / (time / intervalTime)
        // 使用循环定时器不断滚动
        let intervalId = setInterval(function () {
            distance -= itemDistance
            // 到达顶部, 停止定时器
            if (distance <= 0) {
                distance = 0
                clearInterval(intervalId)
            }
            $page.scrollTop(distance)
        }, intervalTime)
    })
})